package com.mingte.manghe.support.service.impl;


import com.mingte.manghe.support.utils.GenUtils;
import com.mingte.manghe.support.dao.SysGeneratordao;
import com.mingte.manghe.support.service.SysGeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

@Service
public class SysGeneratorServiceImpl implements SysGeneratorService {

	@Autowired
	private SysGeneratordao sysGenDao;
	
	@Override
	public Map<String, Object> queryTable(String tableName) {
		return sysGenDao.queryTable(tableName);
	}

	@Override
	public List<String> queryAllTableName() {
		return sysGenDao.queryAllTableName();
	}

	@Override
	public List<Map<String, Object>> queryList() {
		return sysGenDao.queryList();
	}

	@Override
	public List<Map<String, String>> queryColumns(String tableName) {
		return sysGenDao.queryColumns(tableName);
	}

	@Override
	public byte[] code(List<String> tableArr) {
		
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		
		for (String tableName : tableArr) {
			//查询表信息
			Map<String, Object> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			
			GenUtils.generatorCode(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

}
